#!/usr/bin/env bash

if [ "${KB_CN_LEADER}" == "" ]; then
  log:warning "No primary found, waiting for first coordinate."
  KB_CN_LEADER="${KB_CLUSTER_COMP_NAME}-0"
fi

MASTER_SERVICE="${KB_CN_LEADER}.${KB_CLUSTER_COMP_NAME}-headless"

# waiting for master coordinator.
"${CUR_PATH}/wait_for_connection" "${MASTER_SERVICE}" "${PG_PORT}"
assert $? "Error while waiting for connection. Deployment not ready." || exit $?

if [ ! -f "${PGDATA}/postgresql.conf" ]; then
  log:info "Database configuration not found, creating base backup and setting to standby"

  pg_basebackup -d "postgresql://${PGUSER}:${PGPASSWORD}@${MASTER_SERVICE}:${PG_PORT}" -D "${PGDATA}" -P --wal-method=stream

  echo "standby_mode          = 'on'" >"${PGDATA}/recovery.conf"
  echo "primary_conninfo      = 'host=${MASTER_SERVICE} port=${PG_PORT} user=${PGUSER} password=${PGPASSWORD}'" >>"${PGDATA}/recovery.conf"
  echo "trigger_file          = '/tmp/MasterNow'" >>"${PGDATA}/recovery.conf"
  echo "hot_standby = on" >>"${PGDATA}/postgresql.conf"
fi

# required for postgres permissions.
# the loaded values are actually set by kuberntes. :(
log:info "Set data folder permissions 0700"
chmod -R 0700 "${PGDATA}"
assert $? "Failed to change data folder permissions" || exit $?
